<div class="container_wrapper customer_register_default" id="customer_register">
    <div class="mo-card-content">
        <div class="mo-header mo-header-account">
            <div class="header-title">
                <span>{{lang.account.title.create}}</span>
            </div>
            <div class="header-divider"></div>
        </div>

        <form class="mo-form" id="register-form">
            <div class="mo-form-item" name="first_name">
                <div class="mo-form-item-value">
                    <input class="mo-form-input" placeholder="{{lang.account.form.first_name}}" type="text" />
                </div>
            </div>
            <div class="mo-form-item" name="last_name">
                <div class="mo-form-item-value">
                    <input class="mo-form-input" placeholder="{{lang.account.form.last_name}}" type="text" />
                </div>
            </div>
            {% if storeConfig.checkout_contact %}
            <div class="mo-form-item" name="contact">
                <div class="mo-form-item-value">
                    <input class="mo-form-input" placeholder="{{ storeConfig.other_concat | default : 'Whatsapp / Skype' }}" type="text" />
                </div>
            </div>
            {% endif %}
            <div class="mo-form-item" name="email">
                <div class="mo-form-item-value">
                    <input class="mo-form-input" placeholder="{{lang.account.form.email}}" type="text" />
                </div>
            </div>
            <div class="mo-form-item" name="password">
                <div class="mo-form-item-value">
                    <input class="mo-form-input" autocomplete placeholder="{{lang.account.form.password}}" type="password" />
                </div>
            </div>
            {% if storeConfig.checkout_subscription > 0 %}
            <div class="mo-form-item checkout-main">
            
                <label class="checkbox_label mo-checkbox" for="sub_checkbox">  
                    <input class=" mo-checkbox_input order_checkbox insurance_checkbox" type="checkbox" {% if storeConfig.checkout_subscription == "1" %}checked{% endif %} name="sub_checkbox" value="1" id="sub_checkbox"/>
                    <span class="mo-checkbox_label check_icon">{% include icon_order_checkbox %}</span>{{lang.account.create.sub}} 
                </label>
            </div>
            {% endif %}
            <div class="mo-form-item">
                <div class="mo-form-item-value">
                    <div id="register" class="mo-loading-warp main_btn form_btn">{{lang.account.create.register}}</div>
                </div>
            </div>
            <div class="mo-form-item">
                <span class="text-tip"><a href="/account/login">{{lang.account.title.log}}</a></span>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript">
    (function () {
        const rules = {
            email: [{ required: true, message: "{{lang.account.ruls.email}}" },{
                validator:function(value, rules,callback){
                    var emailPat=/^(.+)@(.+)$/;
                    var matchArray=value.match(emailPat);
                    if(!matchArray){
                        return callback("{{lang.account.ruls.email_invalid}}");
                    }
                }
            }],
            password: [{ required: true, message: "{{lang.account.ruls.password}}" },{
                validator:function(value, rules,callback){
                    if(value.length>15 || value.length<6){
                        return callback("{{lang.account.ruls.password_invalid}}");
                    }
                }
            }],
            contact: [{ required: true, message: "{{lang.account.ruls.contact}}" }],
            first_name: [{ required: true, message: "{{lang.account.ruls.first_name}}" }],
            last_name: [{ required: true, message: "{{lang.account.ruls.last_name}}" }]
        };
        const form = moi.form("#register-form", rules);
        const checkoutUrl =  moi.getUrlParam('checkout_url') || "{{checkout_url}}" || '{{routes.account_url}}';
        //注册
        $("#register").click(function () {
            const is_sub = $("#sub_checkbox").get(0).checked;
            if (form.allVerify()) {
                //显示加载
                const load = moi.nodeShowLoading(this);
                const params = moi.paramsValueString(form.allValue());
                params["is_sub"] = is_sub ? 1 : 0;
                moi.ajax( {
                    url: "/account/register",
                    type: "post",
                    data: JSON.stringify(params),
                    complete: function () {
                        load.close();
                    },
                    success: function (data) {
                        //错误提示
                        if (!data.code) {
                            callback_sign_up();
                            callback_generate_lead();
                            moi.message({ content: '{{ lang.customers.login.registered_successfully }}',onClose(){
                                moi.href(checkoutUrl);

                            }});
                        } else {
                            return moi.alert({ title: data.msg });
                        }
                    }
                });
            }
        });
    })();
</script>
